package com.hao.mydownloadlist.service.utils;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;

import java.io.File;
import java.io.IOException;

/**
 * Created by hao on 16/11/21.
 */
public class DownloadPath {

    private static final String TAG = DownloadPath.class.getSimpleName();

    public static final String MUSIC = "music";
    public static final String APP_APK = "apk";
    public static final String VIDEO = "video";


    //应用的总的文件夹名
    private String appSummaryPath;
    //应用子文件夹名字
    private String typeName = MUSIC;
    private Context context;


    public DownloadPath(Context context) {
        this.context = context;
        this.appSummaryPath = "download_test";
    }

    public void setTypeName(String typeName) {
        this.typeName = typeName;
    }

    public void setAppSummaryPath(String appSummaryPath) {
        this.appSummaryPath = appSummaryPath;
    }

    public String getApplicationName() {
        PackageManager packageManager = null;
        ApplicationInfo applicationInfo = null;
        try {
            packageManager = context.getApplicationContext().getPackageManager();
            applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            applicationInfo = null;
        }
        return (String) packageManager.getApplicationLabel(applicationInfo);
    }

    /**
     * 获取设置的路径
     *
     * @param name 文件的名字
     * @param type 文件的类型
     * @return
     */
    public File getAllPath(String name, String type) {
        String path = "";
        if (TextUtils.isEmpty(typeName)) {
            path = getSDCardPath() + File.separator + appSummaryPath;
//            File pathFile = new File(path);
//            if (!pathFile.exists()) {
//                pathFile.mkdirs();
//            }
        } else {
//            path = getSDCardPath() + File.separator + appSummaryPath;
//            File pathFile = new File(path);
//            if (!pathFile.exists()) {
//                try {
//                    pathFile.createNewFile();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
            path = getSDCardPath() + File.separator + appSummaryPath + File.separator + typeName;
        }
        File pathFile = new File(path);
        if (!pathFile.exists()) {
            pathFile.mkdirs();
        }
        Log.i(TAG, ">>>>path>>>" + path);

        File updateFile = new File(path + File.separator + name + "." + type);
        if (!updateFile.exists()) {
            try {
                updateFile.createNewFile();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return updateFile;
    }

    // 获取SDCard的目录路径功能
    public String getSDCardPath() {
        File sdcardDir = null;
        // 判断SDCard是否存在
        boolean sdcardExist = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
        if (sdcardExist) {
            sdcardDir = Environment.getExternalStorageDirectory();
        }
        return sdcardDir.toString();
    }

    /**
     * 销毁
     */
    public void onDestroy() {
        this.context = null;
    }
}
